/***
 **                                                          _ooOoo_
 **                                                         o8888888o
 **                                                         88" . "88
 **                                                         (| -_- |)
 **                                                          O\ = /O
 **                                                      ____/`---'\____
 **                                                    .   ' \\| |// `.
 **                                                     / \\||| : |||// \
 **                                                   / _||||| -:- |||||- \
 **                                                     | | \\\ - /// | |
 **                                                   | \_| ''\---/'' | |
 **                                                    \ .-\__ `-` ___/-. /
 **                                                 ___`. .' /--.--\ `. . __
 **                                              ."" '< `.___\_<|>_/___.' >'"".
 **                                             | | : `- \`.;`\ _ /`;.`/ - ` : | |
 **                                               \ \ `-. \_ __\ /__ _/ .-` / /
 **                                       ======`-.____`-.___\_____/___.-`____.-'======
 **                                                          `=---='
 **
 **                                       .............................................
 **                                              佛祖保佑             永无BUG
 **                                      佛曰:
 **                                              写字楼里写字间，写字间里程序员；
 **                                              程序人员写程序，又拿程序换酒钱。
 **                                              酒醒只在网上坐，酒醉还来网下眠；
 **                                              酒醉酒醒日复日，网上网下年复年。
 **                                              但愿老死电脑间，不愿鞠躬老板前；
 **                                              奔驰宝马贵者趣，公交自行程序员。
 **                                              别人笑我忒疯癫，我笑自己命太贱；
 **                                              不见满街漂亮妹，哪个归得程序员？
 */
/**
 * Created by liang.shan on 2021/06/11
 */

/**
 * 获取所有已安装的插件列表
 */
import { compareVersion } from '../../utils/kit'
const modulePlugins = {
  namespaced: true,
  state: {
    local: [], // 本地插件
    main: {}, // 插件中心
    package: [], // 通过安装包 安装的插件
    normal: [], // 从插件中心下载安装的插件
    needUpgrade: {}, // 需要升级的插件列表
    upgrading: {}, // 正在升级中的插件列表
    downloading: {}, // 正在 下载中的插件
  },
  getters: {
  },
  actions: {
    async init ({ state }) {
      let response = bbtools.sendSync('plugin-list-init')
      state.local = response.local
      state.main = response.main
      state.package = response.package
      state.normal = response.normal
    },
    add ({ state }, {
      type = '',
      data = {}
    }) {
      if (['needUpgrade', 'upgrading', 'downloading'].indexOf(type) > -1) {
        if (!state[type].hasOwnProperty(data.pid) || (compareVersion(data.version, state[type][data.pid].version) === 1)) {
          state[type] = Object.assign({}, state[type][data.pid], {
            [data.pid]: JSON.parse(JSON.stringify(data))
          })
        }
      } else if (state[type] && type != 'main' && data.pid) {
        let _index = -1
        for (let i = 0; i < state[type].length; i++) {
          if (state[type][i].pid === data.pid) {
            _index = i
            break
          }
        }
        if (_index > -1) {
          state[type].splice(_index, 1, data)
        } else {
          state[type].unshift(data)
        }
      }
    },
    remove ({ state }, {
      type = '',
      data = {}
    }) {
      if (['needUpgrade', 'upgrading', 'downloading'].indexOf(type) > -1) {
        if (state[type][data.pid]) {
          state[type] = Object.assign({}, state[type][data.pid], {
            [data.pid]: null
          })
          delete state[type][data.pid]
        }
      } else if (state[type] && type != 'main' && data.pid) {
        let _index = -1
        for (let i = 0; i < state[type].length; i++) {
          if (state[type][i].pid == data.pid) {
            _index = i
            break
          }
        }
        if (_index > -1) {
          state[type].splice(_index, 1)
        }
      }
    },
    update ({ state }, {
      type = '',
      data = {}
    }) {
      if (['needUpgrade', 'upgrading', 'downloading'].indexOf(type) > -1) {
        if (state[type].hasOwnProperty(data.pid)) {
          state[type][data.pid] = Object.assign({}, state[type][data.pid], {
            [data.pid]: JSON.parse(JSON.stringify(data))
          })
        }
      } else if (state[type] && data.pid) {
        let _data = JSON.parse(JSON.stringify(data))
        if (type == 'main') {
          state.main = _data
        } else {
          let _index = -1
          for (let i = 0; i < state[type].length; i++) {
            if (state[type][i].pid == data.pid) {
              _index = i
              break
            }
          }
          if (_index > -1) {
            state[type].splice(_index, 1, _data)
          }
        }
      }
    },

  }
}

export default modulePlugins
